package com.asom.player.service;

import java.io.IOException;
import java.io.Serializable;

import android.media.MediaPlayer;

public class Song implements Comparable<Song>, Serializable
{		
	private static final long serialVersionUID = 1L;

	transient MediaPlayer         	player;
	
	String 			album;
	String			albumId;
	String			albumArt;
	String			artist;
	String			title;
	String			path;
	String			displayName;
	int				duration = -1;

	void prepare()
	{
		try {
			player.reset();	
			player.setDataSource(path);
			player.prepare();
		
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Song(
			String _album,
			String _albumId,
			String _artist,
			String _title,
			String _path,
			String _displayName,
			String _duration)
	{	
		player = new MediaPlayer();
		
		album = _album;
		albumId = _albumId;
		artist = _artist;
		title = _title;
		path = _path;	
		displayName = _displayName;
		
		try
		{
			duration = Integer.parseInt(_duration);
		}
		catch(NumberFormatException _ex)
		{
			
		}
	}
	
	public String getAlbum()
	{
		return album;
	}
	
	public String getAlbumId()
	{
		return albumId;
	}
	
	public void setAlbumArt(String _albumArt)
	{
		albumArt = _albumArt;
	}
	
	public String getAlbumArt()
	{
		return albumArt;
	}
	
	public String getArtist()
	{
		return artist;
	}
	
	public String getTitle()
	{
		return title;
	}
	
	public String getPath()
	{
		return path;
	}
	
	public String getDisplayName()
	{
		return displayName;
	}
	
	public int getDuration()
	{
		return duration;
	}
	
	public void play()
	{
		this.prepare();
		
		player.start();
	}
	
	public void pause()
	{
		player.pause();
	}
	
	public void stop()
	{
		player.stop();
	}
	
	public boolean isPlaying()
	{
		return player.isPlaying();
	}
	
	public void seek(int _msTime)
	{
		player.seekTo(_msTime);
	}
	
	public int getCurrentPosition()
	{
		return player.getCurrentPosition();
	}

	@Override
	public int compareTo(Song another)
	{
		// TODO Auto-generated method stub
		return this.getPath().compareTo(another.getPath());
	}
}
